package com.wu.Backstage.controller;


import cn.hutool.core.date.DateUtil;
import com.wu.Backstage.check.MiddlelayerCheck;
import com.wu.Backstage.entity.Po.UserRecord;
import com.wu.Backstage.entity.Po.UserSearch;
import com.wu.Backstage.entity.Po.adminPo;
import com.wu.Backstage.entity.UserAccount;
import com.wu.Backstage.service.CommodityService;
import com.wu.Backstage.service.UserService;
import com.wu.Backstage.service.comprehensiveService;
import com.wu.Backstage.tool.Tool;
import com.wu.Backstage.util.JsonUtil;
import com.wu.Backstage.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;


/*
 * @param statu
 * @param code
 * @param message
 * @author wujian
 * */

@Controller
@RequestMapping(value = "/user")
public class UserController {

    @Autowired
    private UserService userService;


    @Autowired
    private MiddlelayerCheck oftenCheckf;

    @Autowired
    private HttpServletResponse response;

    @Autowired
    private redisController redisController;

    @Autowired
    private com.wu.Backstage.service.comprehensiveService comprehensiveService;



    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private CommodityService commodityService;


    @RequestMapping("/login")
    public String index() {
        return "login";
    }

    /**
     * 根据给定的用户名查询
     **/
    @RequestMapping(value = "getUserName", method = RequestMethod.GET)
    @ResponseBody
    public void findUserName(String name) {
        Tool reresult = oftenCheckf.findUserByName(name);
        oftenCheckf.Tres(reresult);
    }


    /**
     * 根据给定的email查询
     **/
    @RequestMapping(value = "getUserEmail")
    @ResponseBody
    @CrossOrigin
    public void findUserEmail(String email) {
        Tool reresult = oftenCheckf.findUserByEmial(email);
        System.out.println(reresult);
        oftenCheckf.Tres(reresult);
    }

    /**
     * 根据指定id删除数据
     * **/
    @RequestMapping(value = "DataleUsre")
    @ResponseBody
    @CrossOrigin
    public void findUserEmail(Integer id) {
        Tool reresult = oftenCheckf.DataleUsre(id);
        oftenCheckf.Tres(reresult);
    }

    /**
     * 根据用户id查询数据
     */
    @RequestMapping(value = "getUserId")
    @ResponseBody
    public void  findUserId(String id) {
        Integer ids = Integer.parseInt(id);
        Tool reresult = oftenCheckf.findUserId(ids);
        oftenCheckf.Tres(reresult);
    }

    /**
     * 获取前端的账号密码进行验证
     **/
    @RequestMapping(value = "/loginPage", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    @CrossOrigin
    private void login(HttpServletRequest request) {
        Tool tools;
        String email = request.getParameter("email");
        String password = request.getParameter("password");
        UserAccount userAccount=userService.getAllUserEmail(email);
        if(userAccount==null)
        {
            tools = oftenCheckf.isUserAccount(null,"当前账号未注册,请注册后再试!");
            oftenCheckf.Tres(tools);
            return;
        }
        if(userAccount.getStatuscode()==0)
        {
            tools = oftenCheckf.isUserAccount(null,"当前用户登录异常已被限制登录");
            oftenCheckf.Tres(tools);
            return;
        }

         UserAccount userAccount1 = userService.loginPage(email, password);
         if(userAccount1==null)
         {
             String msg =  redisController.counter(email);
             tools = oftenCheckf.isUserAccount(null,msg);
             oftenCheckf.Tres(tools);
             return;
         }
        Date date = DateUtil.date();
        UserRecord user = new UserRecord();
        user.setEmail(email);
        user.setLogintime(date);
        comprehensiveService.InUserRecord(user);
        tools = oftenCheckf.isUserAccount(userAccount,"登录成功!");
        redisUtil.del(email);
        oftenCheckf.Tres(tools);
        return;
    }

    /**
     * 验证前端昵称和邮箱
     **/
    @RequestMapping(value = "/findEmailAndName", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    @CrossOrigin
    private void findEmailAndName(HttpServletRequest request)
    {
        Tool tools;
        String email = request.getParameter("email");
        String name = request.getParameter("name");

        if(email==null||name==null)
        {
            tools = oftenCheckf.isUserAccount(null,"当前信息为空！");
            oftenCheckf.Tres(tools);
            return;
        }

        if (userService.getAllUserByName(name) != null) {
            tools = oftenCheckf.isUserAccount(null,"当前用户名已被占用,请切换用户名重试！");
            oftenCheckf.Tres(tools);
            return;
        }
        if (userService.getAllUserEmail(email) != null) {
            tools = new Tool(false,
                    400,
                   "当前邮箱已被注册,请切换邮箱重试！",
                   null);
            Tres(tools);
            return;
        }
        tools = oftenCheckf.isUserAccount(1,null);
        oftenCheckf.Tres(tools);
        return;

    }


    /**
     * 获取前端页面信息，完成用户注册
     **/
    @RequestMapping(value = "/sigunpPage", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    @CrossOrigin
    public void logounPage(String email,String password,String name,String sex,Integer age,String dsc,String statuscode) {

        Integer statuscodes = 1;
        if(statuscode.equals("异常"))
        {
            statuscodes = 0;
        }

        if (userService.getAllUserByName(name) != null) {
                Tool tools = new Tool(false,
                        400,
                        "当前用户名已被占用,请切换用户名重试！",
                        null);
                Tres(tools);
            return;
        }
        if (userService.getAllUserEmail(email) != null) {
            Tool tools = new Tool(false,
                    400,
                    "当前邮箱已被注册,请切换邮箱重试！",
                    null);
            Tres(tools);
            return;
        }

        Tool result = oftenCheckf.logounPage(email, password,name,statuscodes,sex,age,dsc);
        oftenCheckf.Tres(result);
        return;
    }

    /**
     * 批量删除数据
     * **/
    @RequestMapping(value = "/DataleUsres", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    @CrossOrigin
    public void DataleUsres(String menusid){
        Tool result = oftenCheckf.DataleUsres(menusid);
        oftenCheckf.Tres(result);
        return;
    }



    /**修改用户数据**/
    @RequestMapping(value = "/UpdateUser", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    @CrossOrigin
    public void UpdateUser(String email,String name,String password,String statuscode,Integer age,String sex,String dsc,Integer id,String date,String time){
        Tool result = oftenCheckf.UpdateUser(email,name,password,statuscode,age,sex,dsc,id);
        oftenCheckf.Tres(result);
        return;
    }


    /**修改用户数据**/
    @RequestMapping(value = "/UserUpdateUser", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    @CrossOrigin
    public void UserUpdateUser(String name,String password,Integer age,String sex,String dsc,Integer id,String img){
        Tool result = oftenCheckf.UserUpdateUser(name,password,age,sex,dsc,id,img);
        oftenCheckf.Tres(result);
        return;
    }






    /**
     * 数据分页显示
     * **/
    @RequestMapping(value = "/PageHelper", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    @CrossOrigin
    public void getPageHelper(int page,int pagesize)
    {
        Tool result = oftenCheckf.getPageHelper(page,pagesize);
        oftenCheckf.Tres(result);
    }


    /**
     * 用户注册
     **/
    @RequestMapping(value = "/adlogup")
    @ResponseBody
    @CrossOrigin
    public void logup(String name,String email,String password) {

        adminPo admin = new adminPo();
        admin.setEmail(email);
        admin.setPasswrod(password);
        admin.setName(name);
        admin.setTime(null);
        Tool result = oftenCheckf.adaddUser(admin);
        Tres(result);

    }

    /**用户登录**/
    @RequestMapping(value = "/adlogin")
    @ResponseBody
    @CrossOrigin
    public void login(String email,String password) {

        Tool result = oftenCheckf.adlogin(email,password);
        Tres(result);

    }


    public void Tres(Tool result)
    {
        try {
            JsonUtil.toJson(result, response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }





}


